package com.meihua.shellapp.fragment;


import android.content.Intent;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.meihua.shellapp.R;
import com.meihua.shellapp.activity.TestActivity;
import com.meihua.shellapp.activity.login.LoginActivity;
import com.meihua.shellapp.activity.webview.WebActivity;
import com.meihua.shellapp.base.BaseFragment;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.bean.TestContentBean;
import com.meihua.shellapp.common.Constant;
import com.meihua.shellapp.dialog.LoginandRegistrationDialog;
import com.meihua.shellapp.utils.FastClickUtils;
import com.meihua.shellapp.utils.SharedPreferencesUtil;
import com.meihua.shellapp.utils.URL;

import butterknife.Bind;

/**
 * 创客中心 在线测验
 */
public class TestFragment extends BaseFragment {

    @Bind(R.id.test_text)
    TextView mText;
    @Bind(R.id.test_start)
    Button mStart;
    private LoginandRegistrationDialog dialog;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_test;
    }

    @Override
    protected void initView() {
        /*设置部分字体颜色*/
        String str = "<p>&nbsp;&nbsp;&nbsp;&nbsp;通过学习,相信您已经掌握了保险行业的相关基础知识,现在请您花<font color='#4e9ded'>40分钟时间</font>,完成<font color='#4e9ded'>20道选择题</font>,<br/>&nbsp;&nbsp;&nbsp;&nbsp;检查下您的学习效果吧每题<font color='#4e9ded'>5分</font>,<font color='#4e9ded'>60分</font>为及格</p>";

        mText.setText(Html.fromHtml(str));
        mStart.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (!SharedPreferencesUtil.getInstance().getBoolean(getContext(), Constant.ISLOGIN, false)) {
            dialog = new LoginandRegistrationDialog.Builder(getContext())
                    .setClickSure(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(getContext(), LoginActivity.class);
                            startActivity(intent);
                            dialog.dismiss();
                        }
                    })
                    .setClickCencle(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            startActivity(new Intent(getContext(), WebActivity.class).putExtra("url", URL.getUrl().REGISTER_URL));
                            dialog.dismiss();
                        }
                    })
                    .setMessage("请您登录注册！")
                    .show();
            return;
        }
        final Intent in = new Intent();
        switch (view.getId()) {
            case R.id.test_start://开始测试
                boolean isLogin = SharedPreferencesUtil.getInstance().getBoolean(getContext(), Constant.ISLOGIN,false);
                if (!isLogin){
                    dialog = new LoginandRegistrationDialog.Builder(getContext())
                            .setClickSure(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    Intent intent = new Intent(getContext(), LoginActivity.class);
                                    startActivity(intent);
                                    dialog.dismiss();
                                }
                            })
                            .setClickCencle(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    startActivity(new Intent(getContext(), WebActivity.class).putExtra("url", URL.getUrl().REGISTER_URL));
                                    dialog.dismiss();
                                }
                            })
                            .setMessage("请您登录注册！")
                            .show();
                    return;
                }
                if (FastClickUtils.isFastClick()) {
                    OkGo.<String>post(URL.getUrl().GETTESTCONT)
                            .tag(this)
                            .params("member_num", MyApplication.getInstance().getUserId())
                            .execute(new StringCallback() {
                                @Override
                                public void onSuccess(Response<String> response) {
                                    TestContentBean bean = (TestContentBean) MyApplication.getInstance().getBeanForJson(TestContentBean.class, response.body());
                                    if (bean == null) {
                                        return;
                                    }
                                    if (bean.getCode() == 10001) {
                                        Toast.makeText(getActivity(), bean.getMsg(), Toast.LENGTH_SHORT).show();
                                        return;
                                    }
                                    in.setClass(getContext(), TestActivity.class);
                                    startActivity(in);
                                }
                            });
                }
                break;
        }
    }
}